package controllers;

import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import dto.DTOUser;
import dto.GeneralMessageDTO;
import dto.RepositoryDTO;

import beans.InboxBeanRemote;
import beans.RepositoryBeanRemote;
import beans.ResourcesBeanRemote;
import beans.UserBeanRemote;

/**
 * 
 * @author zirbom
 * 
 */

public class InboxController {

	@EJB
	InboxBeanRemote iB;

	@EJB
	RepositoryBeanRemote repB;
	
	@EJB
	UserBeanRemote uB;

	@EJB
	ResourcesBeanRemote rB;

	@ManagedProperty(value = "#{adminMail}")
	GeneralMessageDTO gMDto;

	@ManagedProperty(value = "#{dTOUser}")
	DTOUser userDto;

	@ManagedProperty(value = "#{repositoryDTO}")
	RepositoryDTO repoDto;

	private String popUpAdminInbox;
	private int currentID;
	private String currentContent;
	private String special;
	private boolean size=true;

	private List<GeneralMessageDTO> emails;

	/**
	 * GETTERS AND SETTERS
	 */

	public RepositoryDTO getRepoDto() {

		return repoDto;
	}

	public void setRepoDto(RepositoryDTO repoDto) {

		this.repoDto = repoDto;
	}

	public DTOUser getUserDto() {

		return userDto;
	}

	public void setUserDto(DTOUser userDto) {

		this.userDto = userDto;
	}

	public GeneralMessageDTO getaMDto() {

		return gMDto;
	}

	public void setaMDto(GeneralMessageDTO gMDto) {

		this.gMDto = gMDto;
	}

	public int getCurrentID() {

		return currentID;
	}

	public void setCurrentID(int currentID) {

		this.currentID = currentID;
	}

	public String getCurrentContent() {

		return currentContent;
	}

	public void setCurrentContent(String currentContent) {

		this.currentContent = currentContent;
	}

	public List<GeneralMessageDTO> getEmails() {

		if (userDto.getType().equals("admin")) {
			this.emails = iB.getAdminMailList();
			if(this.emails.size()>0) size=true;
			else size=false;

		}

		if (userDto.getType().equals("user")) {
			this.emails = iB.getUserMailList(userDto.getId(), Integer.parseInt(repoDto.getRepID()));
			if(this.emails.size()>0) 
				size=true;
			else size=false;
		}

		// this.emails = iB.getAdminMailList();
		return emails;
	}

	public void setEmails(List<GeneralMessageDTO> emails) {

		this.emails = emails;

	}

	public String verificaID(String id) {

		currentID = Integer.parseInt(id);

		

		

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				
				if(m.getType().equals("create_repository") || 
						m.getType().equals("transfer_rights") ||
						m.getType().equals("create_user")){
					
					this.special = "submitted";
					
				}
				else
					this.special = "todo";
				
				// /// internationalizare
				HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
						.getExternalContext().getSession(true);
				LocaleController language = (LocaleController) session
						.getAttribute("language");
				String messageBundleName = "errorMessages.errors";
				if (language.getLanguage() == "ro") {
					messageBundleName += "_ro";
				} else if (language.getLanguage() == "hu") {
					messageBundleName += "_hu";
				} else if (language.getLanguage() == "de") {
					messageBundleName += "_de";
				}
				ResourceBundle messageBundle = ResourceBundle
						.getBundle(messageBundleName);
				// ///
				if (m.getType().equals("delete_resource")) {
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in3") + m.getContent() + messageBundle.getString("in12") + rB.getResourceNameById(Integer.parseInt(m.getContent()));

				}
				
				if (m.getType().equals("unlock_resource")) {
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in4") + m.getContent() + messageBundle.getString("in12") + rB.getResourceNameById(Integer.parseInt(m.getContent()));

				}
				
				if (m.getType().equals("create_repository")) {
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in5") + m.getContent();

				}
				
				if (m.getType().equals("delete_repository")) {
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in6") + m.getContent()  + messageBundle.getString("in12") + rB.getResourceNameById(Integer.parseInt(m.getContent()));	

				}
				
				if (m.getType().equals("transfer_rights")) {
					String date[] = m.getContent().split(" ");
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in7") + date[0] + messageBundle.getString("in12") + rB.getResourceNameById(Integer.parseInt(date[1]));

				}
				
				if (m.getType().equals("create_user")) {
				
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in8") + m.getContent();

				}
				
				if (m.getType().equals("invalidate_user")) {
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in9") + m.getIdFolder() + messageBundle.getString("in12") + uB.getUser(Integer.parseInt(m.getContent())).getName();

				}
				
				if (m.getType().equals("user_to_repository")) {
					
					String date[] = m.getContent().split(" ");
					currentContent = messageBundle.getString("in1") + m.getIdSender() + messageBundle.getString("in2") + m.getSender() + messageBundle.getString("in10")+ date[1]  +messageBundle.getString("in11") + date[0];

				}
				
				
			}
		}
		popUpAdminInbox = "Ceva";
		// currentContent = emails.get(id - 1).getContent();
		return null;
	}
	
	public String popUpOK() {
		
		popUpAdminInbox = null;
		return null;
	}
	
	public String manageAccept() {

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				if (m.getType().equals("delete_resource"))
					deleteCurrentRow();

				if (m.getType().equals("unlock_resource"))
					unlockCurrentRow();

				if (m.getType().equals("delete_repository")) {
					deleteCurrentRepository();
				}

				if (m.getType().equals("invalidate_user")) {
					invalidateCurrentUser();

				}

				if (m.getType().equals("user_to_repository")) {
					addUsertoLocalRep();
				}

			}
			

		}
		popUpAdminInbox = null;
		return null;
	}

	private void addUsertoLocalRep() {

		
		
		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				
				String date[] = m.getContent().split(" ");
				
				int idRep = Integer.parseInt(date[0]);
				int idUsr = Integer.parseInt(date[1]);
				
				System.out.println(idRep);
				System.out.println(idUsr);
				
				repB.addUserToRepository(idUsr, idRep);
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}
		// TODO Auto-generated method stub
		
	}

	private void invalidateCurrentUser() {
		
		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				uB.invalidateUser(Integer.parseInt(m.getContent()));
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}
		// TODO Auto-generated method stub

	}

	private void deleteCurrentRepository() {

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				repB.deleteRepository(Integer.parseInt(m.getContent()));
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}
		// TODO Auto-generated method stub

	}

	public String deleteCurrentRow() {

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				rB.deleteResource(Integer.parseInt(m.getContent()),userDto.getId());
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}

		return null;
	}

	public String unlockCurrentRow() {

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				rB.unlockResource(Integer.parseInt(m.getContent()),userDto.getId());
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}

		return null;
	}

	public String denyCurrentRow() {

		for (GeneralMessageDTO m : emails) {
			if (m.getId() == currentID) {
				iB.deleteEntry(currentID);
				currentContent = "";

				getEmails();
			}
		}
		
		popUpAdminInbox = null;
		return null;

	}

	public String cancelContent() {

		this.currentContent = "";
		popUpAdminInbox = null;
		return null;
	}

	public String getPopUpAdminInbox() {
		return popUpAdminInbox;
	}

	public void setPopUpAdminInbox(String popUpAdminInbox) {
		this.popUpAdminInbox = popUpAdminInbox;
	}

	public String getSpecial() {

		return special;
	}

	public void setSpecial(String special) {

		this.special = special;
	}

	public boolean isSize() {

		return size;
	}

	public void setSize(boolean size) {

		this.size = size;
	}
}
